/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroPessoas.java
 *
 * Created on 11/10/2011, 10:58:41
 */
package cliente.view;

import cliente.banco.bd_pessoas;
import estruturas.Pessoa;
import estruturas.IdString;
import extras.LOG;
import extras.jcomboboxmodel;
import extras.mensagens;
import extras.tela_erro;
import extras.validacao;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import localizacao.banco.bd_cidade;
import localizacao.banco.bd_estado;
import localizacao.banco.bd_pais;

public class CadastroPessoa extends javax.swing.JFrame
{

    jcomboboxmodel cidademodel = new jcomboboxmodel();
    jcomboboxmodel estadomodel = new jcomboboxmodel();
    jcomboboxmodel paismodel = new jcomboboxmodel();

    /**
     * Creates new form CadastroPessoas
     */
    public CadastroPessoa()
    {
        try
        {
            initComponents();
            validacao();

            List<IdString> pais = bd_pais.getPais();

            for (IdString a : pais)
            {
                paismodel.addElement(a);
            }
            //jcbPais.setModel(paismodel);

            jcbestado.setEnabled(false);
            jcbcidade.setEnabled(false);
        } catch (SQLException ex)
        {
            mensagens.mensagemErro("Ocorreu um erro!", "Houve uma falha ao capturar os dados dos Países. \nTente novamente mais tarde!");
            LOG.add(ex.getMessage());
        }
    }

    private void validacao()
    {
        jtfNome.addKeyListener(new KeyAdapter()
        {

            public void keyTyped(KeyEvent e)
            {
                validacao.digitoOuLetra(e);
            }
        });


        jtfCPF.addKeyListener(new KeyAdapter()
        {

            public void keyTyped(KeyEvent e)
            {
                validacao.digito(e);
            }
        });
        jtfRG.addKeyListener(new KeyAdapter()
        {

            public void keyTyped(KeyEvent e)
            {
                validacao.digito(e);
            }
        });

        jtfNumero.addKeyListener(new KeyAdapter()
        {

            public void keyTyped(KeyEvent e)
            {
                validacao.digito(e);
            }
        });
        jtfNumero.addKeyListener(new KeyAdapter()
        {

            public void keyTyped(KeyEvent e)
            {
                validacao.digito(e);
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfCPF = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfRG = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfNome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtfddd = new javax.swing.JFormattedTextField();
        jtftelefone = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jtfddd_cel = new javax.swing.JFormattedTextField();
        jtfcelular = new javax.swing.JFormattedTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jtfRua = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jtfNumero = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jtfcep = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaComplemento = new javax.swing.JTextArea();
        jLabel10 = new javax.swing.JLabel();
        jcbestado = new javax.swing.JComboBox();
        jcbcidade = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        lcpf = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Pessoas");
        setMinimumSize(new java.awt.Dimension(535, 433));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados Pessoais", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.red));

        jLabel1.setText("CPF:");

        try {
            jtfCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jtfCPF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                verCPFExiste(evt);
            }
        });

        jLabel2.setText("RG:");

        jLabel3.setText("Nome: ");

        jtfNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfNomeActionPerformed(evt);
            }
        });

        jLabel4.setText("Telefone: ");

        try {
            jtfddd.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jtftelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel5.setText("Celular: ");

        try {
            jtfddd_cel.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jtfcelular.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jtfCPF)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfRG))
                            .addComponent(jtfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(253, 253, 253))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfddd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtftelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfddd_cel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfcelular, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jtfRG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtfddd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtftelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jtfddd_cel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfcelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 380, 140));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados de Contato"));
        jPanel4.setLayout(null);

        jLabel6.setText("Endereço: ");
        jPanel4.add(jLabel6);
        jLabel6.setBounds(20, 20, 52, 14);

        jtfRua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfRuaActionPerformed(evt);
            }
        });
        jPanel4.add(jtfRua);
        jtfRua.setBounds(70, 20, 280, 20);

        jLabel7.setText("Número:");
        jPanel4.add(jLabel7);
        jLabel7.setBounds(20, 50, 50, 20);
        jPanel4.add(jtfNumero);
        jtfNumero.setBounds(70, 50, 64, 20);

        jLabel8.setText("CEP:");
        jPanel4.add(jLabel8);
        jLabel8.setBounds(140, 50, 34, 20);

        try {
            jtfcep.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jtfcep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfcepActionPerformed(evt);
            }
        });
        jPanel4.add(jtfcep);
        jtfcep.setBounds(170, 50, 180, 20);

        jLabel9.setText("Complemento:");
        jPanel4.add(jLabel9);
        jLabel9.setBounds(20, 80, 80, 20);

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        jtaComplemento.setColumns(20);
        jtaComplemento.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
        jtaComplemento.setRows(1);
        jtaComplemento.setTabSize(3);
        jScrollPane1.setViewportView(jtaComplemento);

        jPanel4.add(jScrollPane1);
        jScrollPane1.setBounds(90, 80, 260, 53);

        jLabel10.setText("Estado:");
        jPanel4.add(jLabel10);
        jLabel10.setBounds(20, 150, 37, 14);

        jcbestado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbestadoActionPerformed(evt);
            }
        });
        jPanel4.add(jcbestado);
        jcbestado.setBounds(60, 150, 116, 20);

        jPanel4.add(jcbcidade);
        jcbcidade.setBounds(190, 150, 134, 20);

        getContentPane().add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, 370, 210));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/cancel.png"))); // NOI18N
        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 370, 100, -1));

        lcpf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/user_add.png"))); // NOI18N
        lcpf.setText("Cadastrar");
        lcpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadastrar(evt);
            }
        });
        getContentPane().add(lcpf, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 370, 110, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfNomeActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        this.dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void verCPFExiste(java.awt.event.KeyEvent evt)//GEN-FIRST:event_verCPFExiste
    {//GEN-HEADEREND:event_verCPFExiste
    }//GEN-LAST:event_verCPFExiste

    private void jtfcepActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jtfcepActionPerformed
    {//GEN-HEADEREND:event_jtfcepActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfcepActionPerformed

    private void cadastrar(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cadastrar
    {//GEN-HEADEREND:event_cadastrar
        try
        {
            Pessoa p = new Pessoa();

            p.setCelular(this.jtfcelular.getText());
            p.setComplemento(this.jtaComplemento.getText());
            p.setCpf(this.jtfCPF.getText());
            p.setDdd(this.jtfddd.getText());
            p.setDdd_cel(this.jtfddd_cel.getText());
            p.setKcidade(this.jcbcidade.getSelectedItem());
            //p.setKpessoa(this.jtfcelular.getText());
            p.setNome(this.jtfNome.getText());

            p.setNumero((this.jtfNumero.getText()));

            p.setRg(this.jtfRG.getText());
            p.setRua(this.jtfRua.getText());
            p.setTelefone(this.jtftelefone.getText());
            p.setCep(this.jtfcep.getText());

            if (bd_pessoas.cadastraPessoa(p))
            {
                this.dispose();
            }

        } catch (Exception e)
        {
            tela_erro t = new tela_erro("Erro ao cadastrar pessoa!Preencha os dados pessoais!\n" + e.getMessage() + "\n");
            e.printStackTrace();
        }


    }//GEN-LAST:event_cadastrar

private void jcbestadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbestadoActionPerformed

    try
    {
        List<IdString> estados = bd_cidade.getCidades(this.jcbPais.getSelectedItem(), this.jcbestado.getSelectedItem());

        cidademodel.clear();
        for (IdString a : estados)
        {
            cidademodel.addElement(a);
        }
        jcbcidade.setModel(cidademodel);
        jcbcidade.updateUI();
    } catch (Exception e)
    {
        cidademodel.clear();
        jcbcidade.setModel(cidademodel);
        jcbcidade.updateUI();
    }
    jcbcidade.setEnabled(true);
}//GEN-LAST:event_jcbestadoActionPerformed

    private void jtfRuaActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jtfRuaActionPerformed
    {//GEN-HEADEREND:event_jtfRuaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfRuaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbcidade;
    private javax.swing.JComboBox jcbestado;
    private javax.swing.JTextArea jtaComplemento;
    private javax.swing.JFormattedTextField jtfCPF;
    private javax.swing.JTextField jtfNome;
    private javax.swing.JTextField jtfNumero;
    private javax.swing.JTextField jtfRG;
    private javax.swing.JTextField jtfRua;
    private javax.swing.JFormattedTextField jtfcelular;
    private javax.swing.JFormattedTextField jtfcep;
    private javax.swing.JFormattedTextField jtfddd;
    private javax.swing.JFormattedTextField jtfddd_cel;
    private javax.swing.JFormattedTextField jtftelefone;
    private javax.swing.JButton lcpf;
    // End of variables declaration//GEN-END:variables
}
